package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private FileStorageService fileStorageService;

    /*
     * 图片长传
     *
     * */
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        // 检查参数
        if (multipartFile.isEmpty() || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 上传图片到minio中
        String fileName = UUID.randomUUID().toString().replace("-","");

        String originalFilename = multipartFile.getOriginalFilename();

        String lastFix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileId=null;
        try {
             fileId = fileStorageService.uploadImgFile("", fileName + lastFix, multipartFile.getInputStream());
            log.info("上传图片,fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("此处:WmMaterialServiceImpl文件上传失败");
        }

        // url保存到数据库中

        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUrl(fileId);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        // 构造返回

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {

        // 检查参数
        dto.checkParam();
        // 分页查询
        IPage iPage=new Page(dto.getPage(),dto.getSize());

        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper=new LambdaQueryWrapper();

        // 是否收藏
        if(dto.getIsCollection() !=null && dto.getIsCollection()==1 ){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        // 按照用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());

        // 按照时间倒叙
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        IPage page = page(iPage, lambdaQueryWrapper);

        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());

        result.setData(page.getRecords());

        return  result;
    }
}